<?php
class RolsController extends AppController {

	var $name = 'Rols';
	var $uses = array('Rol','User','RolsUser');

    function beforeFilter () {
        parent::beforeFilter();

		// Aplico el Layout segun la Vista o si es AJAX
		if ($this->RequestHandler->isAjax()) {
			$this->layout = 'ajax';
		}else{
			// Cambio la Vista por la del Rol
			$this->layout = strtolower($this->Session->read('VistaActual'));
		}
    }

	function index() {
		$this->Rol->recursive = 0;
		$this->set('rols', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid rol', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('rol', $this->Rol->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Rol->create();
			if ($this->Rol->save($this->data)) {
				$this->Session->setFlash(__('The rol has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rol could not be saved. Please, try again.', true));
			}
		}
		$people = $this->Rol->Person->find('list');
		$this->set(compact('people'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid rol', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Rol->save($this->data)) {
				$this->Session->setFlash(__('The rol has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rol could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Rol->read(null, $id);
		}
		$people = $this->Rol->Person->find('list');
		$this->set(compact('people'));
	}

	function obtenerroles($id_usuario = null, $id_cliente = null) {
		if (!$id_usuario) {
			$this->Session->setFlash(__('Usuario sin Rol Asignado', true));
			$this->redirect('/');
		}
		if (!$id_cliente) {
			$this->Session->setFlash(__('Cliente no Asignado', true));
			$this->redirect('/');
		}
		// Consulto los Roles del Usuario
		$roles_usuario = $this->User->query(
			"SELECT 
				Rol.id, Rol.name
			FROM 
				rols AS Rol
				INNER JOIN rols_users AS RolsUsers ON RolsUsers.rol_id = Rol.id
			WHERE
				RolsUsers.user_id = '".$id_usuario."' AND RolsUsers.client_id = '".$id_cliente."' 
			ORDER BY 
				Rol.orden
			;"
		);

		$arreglo_roles = array();
		foreach($roles_usuario as $registro){
			$arreglo_roles[$registro['Rol']['id']] = $registro['Rol']['name'];
		}

		return $arreglo_roles;
	}


	function setRolActual($id_rol){
		$roles_usuario = $this->obtenerroles($this->Auth->user('id'),$this->Session->read('ClientesUsuarioLogueado.id'));

		// Si el Rol a cambiar pertenece a los posibles de cambiar, lo cambio
		if (array_key_exists($id_rol, $roles_usuario)) {
			$this->Session->write('VistaActual', $roles_usuario[$id_rol]);

			// Defino como Profesional actual al usuario logueado
			$this->User->setProfesionalActual($this->Session->read('ClientesUsuarioLogueado.id'), $this->Auth->user('id'));
		}
	}

	function getRolActual(){
		$this->autoRender=false;
		return $this->Session->read('VistaActual');
	}

	function creararos(){
		$aro =& $this->Acl->Aro;
		
		//Here's all of our group info in an array we can iterate through
		$groups = array(
			0 => array(
				'alias' => 'Administrador'
			),
			1 => array(
				'alias' => 'Asistente'
			),
			2 => array(
				'alias' => 'Profesional'
			),
		);
		
		//Iterate and create ARO groups
		foreach($groups as $data)
		{
			//Remember to call create() when saving in loops...
			$aro->create();
			
			//Save data
			$aro->save($data);
		}

		//Other action logic goes here...
	}
}
?>